package dt.inlee.system.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import dt.inlee.common.pagination.OrderBean;
import dt.inlee.common.pagination.PageBean;
import dt.inlee.common.pagination.SearchBean;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.system.dao.api.IDxTextMessageDao;
import dt.inlee.system.model.DxTextMessage;
import dt.inlee.system.vo.DxTextMessageBean;

@Service("dxTextMessageService")
public class DxTextMessageService {
	@Resource(name = "dxTextMessageDao")
	private IDxTextMessageDao dxTextMessageDao;

	/**
	 * Add a data
	 * 
	 * @param
	 * @return
	 */
	public String save(DxTextMessage entity) {

		return dxTextMessageDao.save(entity);
	}

	/**
	 * Update a data
	 * 
	 * @param
	 * @return
	 */
	public void update(DxTextMessage entity) {

		dxTextMessageDao.update(entity);
	}

	/**
	 * Show a data
	 * 
	 * @param id
	 * @return
	 */
	public DxTextMessage getDxTempById(String id) {
		return dxTextMessageDao.get(id);
	}

	/**
	 * Delete an object by id
	 * 
	 * @param RecordEntity
	 * @return
	 */
	public void deleteById(String id) {
		dxTextMessageDao.delete(id);
	}

	public PageBean getPage(SearchBean sb, int pageIndex, int pageSize) {

		PageBean pageBean = new PageBean();
		pageBean.setPageIndex(pageIndex);
		pageBean.setPageSize(pageSize);

		if (sb == null) {
			sb = new SearchBean();
		}

		OrderBean orderBean = new OrderBean("dxid", false);
		sb.addOrder(orderBean);

		pageBean.setParams(sb);
		this.dxTextMessageDao.searchByPagination(pageBean);
		// total[0]=pageBean.getTotalData();
		return pageBean;
	}

	public List<DxTextMessage> getList() {
		SearchBean sb = new SearchBean();
		return this.dxTextMessageDao.find(sb);
	}

	public List<DxTextMessage> getList(SearchBean sb) {
		return this.dxTextMessageDao.find(sb);
	}
	/***************************************日月统计报表begin****************************/
	/**
	 * 用于短信日统计报表
	 * 
	 * @return
	 */
	public List<DxTextMessageBean> findListDaySum(String begintime,
			String endtime, Integer[] arrayEmpId, Integer pageIndex,
			Integer pageSize) {
		StringBuffer sb = new StringBuffer(
				"SELECT count(*),M.DAYTIME,M.DXTYPE,M.EMPLOYEEID FROM (SELECT t.*,substr(t.dxcreatedate,0,10) daytime FROM DX_TEXT_MESSAGE t) M");
		sb.append(" WHERE 1 = 1 ");
		if (!BaseUtil.isEmpty(begintime)) {
			sb.append(" AND M.DAYTIME >= '" + begintime + "'");
		}
		if (!BaseUtil.isEmpty(endtime)) {
			sb.append(" AND M.DAYTIME <= '" + endtime + "'");
		}
		if (!BaseUtil.isEmpty(arrayEmpId)) {
			if (arrayEmpId.length == 0) {
				sb.append(" AND 1 != 1");
			} else {
				sb.append(" AND M.EMPLOYEEID IN(");
				for (Integer empid : arrayEmpId) {
					sb.append(empid + ",");
				}
				sb.delete(sb.length() - 1, sb.length());
				sb.append(")");
			}
		}
		sb.append(" GROUP BY M.EMPLOYEEID,M.DAYTIME,M.DXTYPE");
		
		List<Object[]> listObj = this.dxTextMessageDao.findBySql(sb.toString(),
				pageIndex, pageSize);
		List<DxTextMessageBean> listBean = new ArrayList<DxTextMessageBean>();
		DxTextMessageBean bean = null;
		int i = -1;
		for (Object[] objects : listObj) {
			bean = new DxTextMessageBean();
			bean.setCountNum(objects[++i] == null ? null : Integer
					.parseInt(objects[i].toString()));
			bean.setDxdate(objects[++i] == null ? null : objects[i].toString());
			bean.setDxtype(objects[++i] == null ? null : objects[i].toString());
			bean.setEmployeeid(objects[++i] == null ? null : Integer
					.parseInt(objects[i].toString()));
			i = -1;
			listBean.add(bean);
		}
		return listBean;
	}

	/**
	 * 日统计总条数
	 * 
	 * @param begintime
	 * @param endtime
	 * @param enterpriseid
	 * @param arrayEmpId
	 * @return
	 */
	public Integer findListDaySumTotal(String begintime, String endtime,
			Integer[] arrayEmpId) {
		int total = 0;
		StringBuffer sb = new StringBuffer(
				"SELECT COUNT(0) FROM (SELECT count(*) FROM (SELECT t.*,substr(t.dxcreatedate,0,10) daytime FROM DX_TEXT_MESSAGE t) M");
		sb.append(" WHERE 1 = 1 ");
		if (!BaseUtil.isEmpty(begintime)) {
			sb.append(" AND M.DAYTIME >= '" + begintime + "'");
		}
		if (!BaseUtil.isEmpty(endtime)) {
			sb.append(" AND M.DAYTIME <= '" + endtime + "'");
		}
		if (!BaseUtil.isEmpty(arrayEmpId)) {
			if (arrayEmpId.length == 0) {
				sb.append(" AND 1 != 1");
			} else {
				sb.append(" AND M.EMPLOYEEID IN(");
				for (Integer empid : arrayEmpId) {
					sb.append(empid + ",");
				}
				sb.delete(sb.length() - 1, sb.length());
				sb.append(")");
			}
		}
		sb.append(" GROUP BY M.DAYTIME,M.DXTYPE,M.EMPLOYEEID)");
		total = this.dxTextMessageDao.countBySQL(sb.toString());
		return total;
	}

	/**
	 * 用于短信月统计报表
	 * 
	 * @return
	 */
	public List<DxTextMessageBean> findListMonthSum(String year, String month,
			Integer[] arrayEmpId,Integer pageIndex,Integer pageSize) {
		StringBuffer sb = new StringBuffer(
				"SELECT count(*),M.DAYTIME,M.DXTYPE,M.EMPLOYEEID FROM (SELECT t.*,substr(t.dxcreatedate,0,7) daytime FROM DX_TEXT_MESSAGE t) M ");
		sb.append("WHERE 1 = 1 ");
		String daytime = year + "-" + month;
		sb.append(" AND M.DAYTIME = '" + daytime + "' ");
		if (!BaseUtil.isEmpty(arrayEmpId)) {
			if (arrayEmpId.length == 0) {
				sb.append(" AND 1 != 1");
			} else {
				sb.append(" AND M.EMPLOYEEID IN(");
				for (Integer empid : arrayEmpId) {
					sb.append(empid + ",");
				}
				sb.delete(sb.length() - 1, sb.length());
				sb.append(")");
			}
		}
		sb.append(" GROUP BY M.DAYTIME,M.DXTYPE,M.EMPLOYEEID");
		List<Object[]> listObj = this.dxTextMessageDao.findBySql(sb.toString(),
				pageIndex, pageSize);
		List<DxTextMessageBean> listBean = new ArrayList<DxTextMessageBean>();
		DxTextMessageBean bean = null;
		int i = -1;
		for (Object[] objects : listObj) {
			bean = new DxTextMessageBean();
			bean.setCountNum(objects[++i] == null ? null : Integer
					.parseInt(objects[i].toString()));
			bean.setDxdate(objects[++i] == null ? null : objects[i].toString());
			bean.setDxtype(objects[++i] == null ? null : objects[i].toString());
			bean.setEmployeeid(objects[++i] == null ? null : Integer
					.parseInt(objects[i].toString()));
			i = -1;
			listBean.add(bean);
		}
		return listBean;
	}
	
	/**
	 * 日统计总条数
	 * 
	 * @param begintime
	 * @param endtime
	 * @param enterpriseid
	 * @param arrayEmpId
	 * @return
	 */
	public Integer findListMonthSumTotal(String begintime, String endtime,
			Integer[] arrayEmpId) {
		int total = 0;
		StringBuffer sb = new StringBuffer(
				"SELECT COUNT(0) FROM (SELECT count(*) FROM (SELECT t.*,substr(t.dxcreatedate,0,10) daytime FROM DX_TEXT_MESSAGE t) M");
		sb.append(" WHERE 1 = 1 ");
		if (!BaseUtil.isEmpty(begintime)) {
			sb.append(" AND M.DAYTIME >= '" + begintime + "'");
		}
		if (!BaseUtil.isEmpty(endtime)) {
			sb.append(" AND M.DAYTIME <= '" + endtime + "'");
		}
		if (!BaseUtil.isEmpty(arrayEmpId)) {
			if (arrayEmpId.length == 0) {
				sb.append(" AND 1 != 1");
			} else {
				sb.append(" AND M.EMPLOYEEID IN(");
				for (Integer empid : arrayEmpId) {
					sb.append(empid + ",");
				}
				sb.delete(sb.length() - 1, sb.length());
				sb.append(")");
			}
		}
		sb.append(" GROUP BY M.DAYTIME,M.DXTYPE,M.EMPLOYEEID)");
		total = this.dxTextMessageDao.countBySQL(sb.toString());
		return total;
	}
	
	/**
	 * 导出日统计报表
	 * @return
	 */
	public LinkedHashMap<String, String[]> ModelToExcelDay() throws Exception{
		LinkedHashMap<String, String[]> kModel = new LinkedHashMap<String, String[]>();
		kModel.put("empname", new String[]{"员工名称","25"});
		kModel.put("empphone", new String[]{"员工电话号码","25"});
		kModel.put("dxdate", new String[]{"日期","20"});
		kModel.put("countNum", new String[]{"总条数","20"});
		return kModel;
	}
	/**
	 * 导出月统计报表
	 * @return
	 * @throws Exception
	 */
	public LinkedHashMap<String, String[]> ModelToExcelMonth() throws Exception{
		LinkedHashMap<String, String[]> kModel = new LinkedHashMap<String, String[]>();
		kModel.put("empname", new String[]{"员工名称","25"});
		kModel.put("empphone", new String[]{"员工电话号码","25"});
		kModel.put("dxdate", new String[]{"年月","20"});
		kModel.put("countNum", new String[]{"总条数","20"});
		return kModel;
	}
	/***************************************日月统计报表end****************************/

}
